package com.lyl.serviceorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.commonutils.R;
import com.lyl.exception.ZxjyException;
import com.lyl.serviceorder.cloudclient.EduClient;
import com.lyl.serviceorder.cloudclient.ResumeClient;
import com.lyl.serviceorder.cloudclient.UcenterClient;
import com.lyl.serviceorder.entity.Order;
import com.lyl.serviceorder.mapper.OrderMapper;
import com.lyl.serviceorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyl.serviceorder.utils.OrderNoUtil;
import com.lyl.vo.order.CourseWebVoOrder;
import com.lyl.vo.order.ResumeModelVoOrder;
import com.lyl.vo.order.UcenterMemberWebVoOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-25
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private ResumeClient resumeClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private OrderMapper orderMapper;



    //创建订单
    @Override
    @Transactional
    public String saveOrder(Long courseId, Long memberId) {
        QueryWrapper<Order> orderQueryWrapper= new QueryWrapper<>();
        orderQueryWrapper.eq("course_id",courseId);
        orderQueryWrapper.eq("member_id",memberId);

        if(baseMapper.selectCount(orderQueryWrapper)>0) {//  有相同订单
            throw  new ZxjyException("已存在相同课程,请查看购物车");
        }

        //远程调用课程服务，根据课程id获取课程信息
       CourseWebVoOrder courseDto = eduClient.getCourseInfoDto(courseId);

        //远程调用用户服务，根据用户id获取用户信息
        UcenterMemberWebVoOrder ucenterMember = ucenterClient.getInfo(memberId);

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }

    @Override
    public double sumPriceByDate(String date) {
        //

        return orderMapper.sumPriceByDate(date);
    }

    @Override
    public void pageQuery(Page<Order> pageParam, Order courseQuery) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

//        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
//            return;
//        }

//        String title = courseQuery.getTitle();
//        String teacherId = courseQuery.getTeacherId();
//        String subjectParentId = courseQuery.getSubjectParentId();
//        String subjectId = courseQuery.getSubjectId();
//
//        if (!StringUtils.isEmpty(title)) {
//            queryWrapper.like("title", title);
//        }
//
//        if (!StringUtils.isEmpty(teacherId) ) {
//            queryWrapper.eq("teacher_id", teacherId);
//        }
//
//        if (!StringUtils.isEmpty(subjectParentId)) {
//            queryWrapper.ge("subject_parent_id", subjectParentId);
//        }
//
//        if (!StringUtils.isEmpty(subjectId)) {
//            queryWrapper.ge("subject_id", subjectId);
//        }
//
//        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public List<Order> selectList(QueryWrapper<Order> wrapper) {
        return baseMapper.selectList(wrapper);
    }

    @Override
    public String saveOrderResume(Long resumeModelId, long memberId) {
        QueryWrapper<Order> orderQueryWrapper= new QueryWrapper<>();
        orderQueryWrapper.eq("resume_model_id",resumeModelId);
        orderQueryWrapper.eq("member_id",memberId);

        if(baseMapper.selectCount(orderQueryWrapper)>0) {//  有相同订单
            throw  new ZxjyException("已购买相同简历服务,请查看订单");
        }

// 远程调用简历服务, 得到简历模板信息
        ResumeModelVoOrder resumeModelDto= resumeClient.getResumeModelDto(resumeModelId);
        //远程调用用户服务，根据用户id获取用户信息
        UcenterMemberWebVoOrder ucenterMember = ucenterClient.getInfo(memberId);

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setResumeModelId(resumeModelId);
        order.setCourseTitle(resumeModelDto.getTitle());
        order.setCourseCover(resumeModelDto.getCover());

        order.setTotalFee(resumeModelDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
